package com.gavindon.android.widget

import android.content.Context
import android.graphics.Color
import android.graphics.Rect
import android.graphics.drawable.GradientDrawable
import android.graphics.drawable.LayerDrawable
import android.graphics.drawable.ShapeDrawable
import android.graphics.drawable.StateListDrawable
import android.graphics.drawable.shapes.RectShape
import android.graphics.drawable.shapes.RoundRectShape
import android.util.AttributeSet
import android.widget.EdgeEffect
import android.widget.FrameLayout
import android.widget.Toast
import androidx.core.content.ContextCompat
import androidx.leanback.widget.FocusHighlightHelper
import com.gavindon.android.R
import org.jetbrains.anko.dip
import android.graphics.RectF
import android.view.KeyEvent
import android.view.View
import org.jetbrains.anko.toast


/**
 * description:
 * Created by liNan on  2022-2-5 20:55
 */
class BannerFrameLayout : FrameLayout {


    private val stateFocus = android.R.attr.state_focused
    private var gradientColor: IntArray

    constructor(context: Context) : this(context, null)
    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        val ta = context.theme.obtainStyledAttributes(
            attrs,
            R.styleable.GradientDrawableFrameLayout,
            defStyleAttr,
            0
        )
        val startColor =
            ta.getColor(R.styleable.GradientDrawableFrameLayout_color_start, Color.TRANSPARENT)
        val endColor =
            ta.getColor(R.styleable.GradientDrawableFrameLayout_color_end, Color.TRANSPARENT)
        ta.recycle()

        gradientColor = intArrayOf(startColor, endColor)
        init()
    }

    private fun init() {
        background = stateListDrawable()
//        FocusViewUtils.scaleView(this, 0)
    }

    private fun stateListDrawable(): StateListDrawable {
        val stateListDrawable = StateListDrawable()
        stateListDrawable.addState(
            intArrayOf(stateFocus),
            effect()
        )
        stateListDrawable.addState(
            intArrayOf(),
            getGradientDrawable()
        )
        return stateListDrawable
    }

    private fun getGradientDrawable(): GradientDrawable {
        val gradientDrawable =
            GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, gradientColor)
        gradientDrawable.cornerRadius = dip(10).toFloat()
        return gradientDrawable
    }

    private fun effect(): LayerDrawable {
        val corner = dip(10).toFloat()
        val outerR = floatArrayOf(corner, corner, corner, corner, corner, corner, corner, corner)
        val shapeDrawable = ShapeDrawable(RoundRectShape(outerR, null, null))
        shapeDrawable.paint.color = Color.WHITE
        val layers = arrayOf(shapeDrawable, getGradientDrawable())
        val layerDrawable = LayerDrawable(layers)
        layerDrawable.setLayerInset(0, -1, -1, -1, -2)

        return layerDrawable
    }


}